package com.lantosec.hmrp.modules.eval.service.impl;

import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.eval.entity.EvalApplyDetail;
import com.lantosec.hmrp.modules.eval.mapper.EvalApplyDetailMapper;
import com.lantosec.hmrp.modules.eval.service.IEvalApplyDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.eval.vo.EvalApplyDetailVO;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 奖惩申报项目明细 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-23
 */
@Service
@Primary
@Log4j2
public class EvalApplyDetailServiceImpl extends ServiceImpl<EvalApplyDetailMapper, EvalApplyDetail> implements IEvalApplyDetailService {
    @Autowired
    private ISysUploadsService iSysUploadsService;

    @Override
    public List<EvalApplyDetailVO> getDeptExamineAll(PageVO<EvalApplyDetailVO> page) {
        List<EvalApplyDetailVO> evalApplyDetailVOS = baseMapper.getDeptExamineAll(page);
        evalApplyDetailVOS = getEvalApplyDetailVo(evalApplyDetailVOS);
        page.setRecords(evalApplyDetailVOS);
        return evalApplyDetailVOS;
    }

    @Override
    public List<EvalApplyDetailVO> getApplyExamineUser(PageVO<EvalApplyDetailVO> page) {
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        List<EvalApplyDetailVO> evalApplyDetailVOS = baseMapper.getApplyExamineUser(page,userId);
        evalApplyDetailVOS = getEvalApplyDetailVo(evalApplyDetailVOS);
        page.setRecords(evalApplyDetailVOS);
        return evalApplyDetailVOS;
    }

    @Override
    public List<EvalApplyDetailVO> getApplyExamineDept(PageVO<EvalApplyDetailVO> page) {
        //获取当前用户部门id
        String deptId = TokenUtil.getDeptId();
        List<EvalApplyDetailVO> evalApplyDetailVOS = baseMapper.getApplyExamineDept(page,deptId);
        evalApplyDetailVOS = getEvalApplyDetailVo(evalApplyDetailVOS);
        page.setRecords(evalApplyDetailVOS);
        return evalApplyDetailVOS;
    }

    @Override
    public List<EvalApplyDetailVO> getApplyExamineOurDept(PageVO<EvalApplyDetailVO> page) {
        //获取当前用户部门id
        String deptId = TokenUtil.getDeptId();
        List<EvalApplyDetailVO> evalApplyDetailVOS = baseMapper.getApplyExamineOurDept(page,deptId);
        evalApplyDetailVOS = getEvalApplyDetailVo(evalApplyDetailVOS);
        page.setRecords(evalApplyDetailVOS);
        return evalApplyDetailVOS;
    }

    @Override
    public List<EvalApplyDetailVO> getApplyExamineAllDept(PageVO<EvalApplyDetailVO> page) {
        List<EvalApplyDetailVO> evalApplyDetailVOS = baseMapper.getApplyExamineAllDept(page);
        evalApplyDetailVOS = getEvalApplyDetailVo(evalApplyDetailVOS);
        page.setRecords(evalApplyDetailVOS);
        return evalApplyDetailVOS;
    }

    @Override
    public List<EvalApplyDetailVO> getEvalApplyAll(PageVO<EvalApplyDetailVO> page) {
        List<EvalApplyDetailVO> evalApplyDetailVOS = baseMapper.getEvalApplyAll(page);
        evalApplyDetailVOS = getEvalApplyDetailVo(evalApplyDetailVOS);
        page.setRecords(evalApplyDetailVOS);
        return evalApplyDetailVOS;
    }

    @Override
    public List<EvalApplyDetailVO> getDeptExamine(PageVO<EvalApplyDetailVO> page) {
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        List<EvalApplyDetailVO> evalApplyDetailVOS = baseMapper.getDeptExamine(page,userId);
        evalApplyDetailVOS = getEvalApplyDetailVo(evalApplyDetailVOS);
        page.setRecords(evalApplyDetailVOS);
        return evalApplyDetailVOS;
    }

    @Override
    public List<EvalApplyDetailVO> getUserExamine(PageVO<EvalApplyDetailVO> page) {
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        List<EvalApplyDetailVO> evalApplyDetailVOS = baseMapper.getUserExamine(page,userId);
        evalApplyDetailVOS = getEvalApplyDetailVo(evalApplyDetailVOS);
        page.setRecords(evalApplyDetailVOS);
        return evalApplyDetailVOS;
    }

    @Override
    public void updateDeptExamine(EvalApplyDetailVO vo) {
        updateDeptExamines(vo);
    }

    private void updateDeptExamines(EvalApplyDetailVO vo) {
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String name = TokenUtil.getName();
        EvalApplyDetail evalApplyDetail = vo.convertToEntity();
        evalApplyDetail.setFinAuditorId(userId);
        evalApplyDetail.setFinAuditor(name);
        evalApplyDetail.setFinAuditTime(DateUtils.nowDate());
        this.updateById(evalApplyDetail);
    }

    @Override
    public void updateUserExamine(EvalApplyDetailVO vo) {
        updateUserExamines(vo);
    }

    private void updateUserExamines(EvalApplyDetailVO vo) {
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String name = TokenUtil.getName();
        EvalApplyDetail evalApplyDetail = vo.convertToEntity();
        evalApplyDetail.setDeptAuditorId(userId);
        evalApplyDetail.setDeptAuditor(name);
        evalApplyDetail.setDeptAuditTime(DateUtils.nowDate());
        this.updateById(evalApplyDetail);
    }

    @Override
    public void updateDeptExamineList(List<EvalApplyDetailVO> voList) {
        voList.forEach(param->{
            updateDeptExamines(param);
        });
    }

    @Override
    public void updateUserExamineList(List<EvalApplyDetailVO> voList) {
        voList.forEach(param-> {
            updateUserExamines(param);
        });
    }

    /**
     * 封装EvalApplyDetailVO数据
     * @param evalApplyDetailVOS
     */
    private List<EvalApplyDetailVO> getEvalApplyDetailVo(List<EvalApplyDetailVO> evalApplyDetailVOS) {
        evalApplyDetailVOS.forEach(param->{
            String photoList = param.getPhotoList();
            if(StringUtils.isNotEmpty(photoList)){
                String[] photoLists = photoList.split(",");
                List<String> savepath = new ArrayList<>();
                for(int i = 0;i<photoLists.length;i++) {
                    SysUploads sysUploads = iSysUploadsService.getById(photoLists[i]);
                    String photoListPath = InitDataListener.uploadsUrl + sysUploads.getSavePath() + sysUploads.getFileName();
                    savepath.add(photoListPath);
                }
                param.setSavepath(savepath);
            }
        });
        return evalApplyDetailVOS;
    }
}
